/*=========================================================================
The Impact of Denying a Wanted Abortion on Women and Children
Authors: Juliana Londoño-Vélez and Estefanía Saravia

Creation date: January 9th, 2024
--------------------------------------------------------------------------
Table 5 - Impacts of Denying a Wanted Abortion on Childbearing and Mortality
=========================================================================*/

use "$data/Final_EEVV.dta", replace

* Panel A
global outcomes Parir_9meses Defuncion_Mama9meses Aborto_Related Defuncion_Materna_DANE Other External Pario_Murio_9meses 

mat 	a = J(10,4,.)

matrix rownames a =  $outcomes Parir_Despues Defuncion_MamaAfter Nuevo_Aborto

local c=1
foreach var in  $outcomes { 

sum `var' if Niega==0
mat 	a[`c',1] = r(mean)

ivreghdfe `var'         (Niega=Mujer_Ponente)  , abs(oficina#time) cluster(id_juez) savefirst

mat 	a[`c',2]  = _b[Niega]
mat 	a[`c',3] = _se[Niega]

mat 	a[`c',4] =e(N)


local c=`c'+1
}

* Panel B
local c=8
foreach var in  Parir_Despues Defuncion_MamaAfter {


sum `var' if Niega==0
mat 	a[`c',1] = r(mean)

ivreghdfe `var'         (Niega=Mujer_Ponente) if Sample==1 , abs(oficina#time) cluster(id_juez) savefirst

mat 	a[`c',2]  = _b[Niega]
mat 	a[`c',3] = _se[Niega]

mat 	a[`c',4] =e(N)


				
local c=`c'+1
}

local c=10
foreach var in  Nuevo_Aborto {

sum `var' if Niega==0
mat 	a[`c',1] = r(mean)

ivreghdfe `var'         (Niega=Mujer_Ponente)  , abs(oficina#time) cluster(id_juez) savefirst

mat 	a[`c',2]  = _b[Niega]
mat 	a[`c',3] = _se[Niega]

mat 	a[`c',4] =e(N)


local c=`c'+1
}



mat lis a

	svmat2 a, rnames(outcome)
	
	drop if outcome==""
	
	rename a1 mean_nondenied
	rename a2 coef_femaleIV
	rename a3 se_femaleIV
	rename a4 N_femaleIV
	
	keep outcome mean_nondenied coef_femaleIV se_femaleIV N_femaleIV 
	
	
	order outcome mean_nondenied coef_femaleIV se_femaleIV N_femaleIV 
	
	export excel using "$output/Tables paper matrices replication.xlsx", sheet("Table 5", modify) 
